using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace LynxWebLog.Areas.Blogs
{
    public class BlogsAreaAreaRegistration : AreaRegistration 
	{
        public override string AreaName 
		{
            get 
			{
                return "Blogs";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) 
		{

            context.MapRoute(
                name: "ManifestFile",
                url: "Blogs/{username}/{blogname}/wlwmanifest.xml",
                defaults: new { controller = "Blog", action = "ManifestFile" },
                constraints: new
                {
                    username = new BlogOwnerRouteConstraint(),
                    blogname = new BlogNameRouteConstraint()
                }
            );

            context.MapRoute(
                name: "Blog_CurrentDisplay",
                url: "Blogs/{username}/{blogname}",
                defaults: new { controller = "Blog", action = "ViewCurrent" },
                constraints: new
                {
                    username = new BlogOwnerRouteConstraint(),
                    blogname = new BlogNameRouteConstraint()
                }
            );

            context.MapRoute(
                "Blogs_default",
                "Blogs/{username}/{blogname}/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional },
                constraints: new 
                { 
                    username = new BlogOwnerRouteConstraint(),
                    blogname = new BlogNameRouteConstraint()
                }
            );

        }
    }

    public class BlogOwnerRouteConstraint : IRouteConstraint
    {
        private readonly BlogSupport bs;

        public BlogOwnerRouteConstraint()
        {
            bs = new BlogSupport();
        }

        public bool Match(HttpContextBase httpContext, Route route,
            string parameterName, RouteValueDictionary values,
            RouteDirection routeDirection)
        {
            
            string username = values["username"].ToString();
            return AsyncHelpers.RunSync<bool>(() => bs.Exists(username));
        }
    }

    public class BlogNameRouteConstraint : IRouteConstraint
    {
        private readonly BlogSupport bs;

        public BlogNameRouteConstraint()
        {
            bs = new BlogSupport();
        }

        public bool Match(HttpContextBase httpContext, Route route,
            string parameterName, RouteValueDictionary values,
            RouteDirection routeDirection)
        {
            string username = values["username"].ToString();
            string blogname = values["blogname"].ToString();
            return AsyncHelpers.RunSync<bool>(() => bs.BlogExists(username, blogname));
        }
    }


}